//index.js
//获取应用实例
var app = getApp();
var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
Page({
    data: {
        dataLoaded: true,
        name: '',
        phone: '',
        content: '',
        userInfo: {},
        shopList: [],
        listShow: false,
        shopName: '',
        shopId: '',
        leftWordCount: 140
    },
    onLoad: function() {
        var _self = this;
        app.getUserInfo(function(userInfo) {
            _self.setData({
                userInfo
            });
        });
        this.getShopList();
    },
    /**
     * 商户列表
     */
    getShopList: function() {
        var _self = this;
        this.setData({
            dataLoaded: false
        })
        ajax.G('/openapi/User/getUserComplainShopList', {}, function(data) {
            _self.setData({
                shopList: data.result.shopList,
                dataLoaded: true
            })
        })
    },
    _switchShopList: function() {
        var listShow = this.data.listShow;
        this.setData({
            listShow: !listShow
        })
    },
    _selectTab: function(evt) {
        var index = +evt.currentTarget.dataset.current;
        var shopId = evt.currentTarget.dataset.shopId;
        var shopName = evt.currentTarget.dataset.shopName;
        this.setData({
            shopListCurrent: index,
            shopName: shopName,
            shopId: shopId
        })
    },
    /**
     * 取消
     */
    _cancel: function() {
        wx.navigateBack();
    },
    _monitorName: function(evt) {
        var name = evt.detail.value;
        this.setData({
            name: name
        })
    },
    _monitorPhone: function(evt) {
        var phone = evt.detail.value;
        this.setData({
            phone: phone
        })
    },
    _monitorContent: function(evt) {
        var content = evt.detail.value;
        this.setData({
            leftWordCount: 140 - content.length,
            content: content
        })
    },
    /**
     * 新建、修改
     */
    _submit: function(evt) {
        var _self = this;
        var {
            name,
            phone,
            store,
            content
        } = evt.detail.value;

        var parmars = {
            name: name,
            phone: phone,
            shopid: this.data.shopId,
            content: content
        }
        var regTel = /^1[34578]\d{9}$/;
        regTel = regTel.exec(phone);
        if (name.length == 0) {
            util.alert('请输入姓名');
            return false;
        } else if (phone.length == 0) {
            util.alert('请输入手机号');
            return false;
        } else if (!regTel) {
            util.alert('手机号格式不正确');
            return false;
        } else if (store.length == 0) {
            util.alert('请选择您要投诉的店铺');
            return false;
        }
        this.setData({
            dataLoaded: false
        })
        ajax.P('/openapi/User/userComplain', parmars, function(data) {
            _self.setData({
                dataLoaded:true
            })
            util.alert('提交成功', function() {
                wx.navigateBack();
            });
        })
    }
})